package fifth.引用传递.接受本类引用传递的范例.范例一;

/**
 * 一对一关系
 * 一个人有一本书 书是属于一个人的
 * 得出结论：
 * 人是一个具体的类 书也是一个具体的类
 * 在人的类中应该有一个属性表示书
 * 在书的类中应该有一个属性表述人
 *
 */
class Person {
    private String name;
    private int age;
    private Book book;

    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }
}

class Book {
    private String title;
    private double price;
    private Person person;

    public Book(String title, double price) {
        this.title = title;
        this.price = price;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public Person getPerson() {
        return person;
    }

    public void setPerson(Person person) {
        this.person = person;
    }
}

public class RefCase {
    public static void main(String[] args) {
        Person p = new Person("odyssey", 18);
        Book b = new Book("Java从入门到入土", 91.2);
        p.setBook(b);
        b.setPerson(p);
        System.out.println("从人找到书->姓名：" + p.getName() + " 年龄：" + p.getAge() + " 书名：" + p.getBook().getTitle() + " 价格：" + p.getBook().getPrice());
        System.out.println("从书找到人->书名：" + b.getTitle() + " 价格:" + b.getPrice() + " 人名：" + b.getPerson().getName() + " 年龄：" + b.getPerson().getAge());
    }
};

